package com.flyee.controller {
	
	import com.flyee.controller.timer.CartoonTimer;
	import com.flyee.controller.timer.MonsterTimer;
	import com.flyee.controller.timer.WalkTimer;
	import com.flyee.controller.tools.cartoon.model.AppearanceConfig;
	import com.flyee.controller.tools.monster.control.CutMananger;
	import com.flyee.controller.tools.path.AStar;
	import com.flyee.model.Charactor;
	import com.flyee.model.Monster;
	import com.flyee.service.CharactorService;
	import com.flyee.service.pool.EffectDepot;
	import com.flyee.service.pool.MonsterDepot;
	import com.flyee.service.pool.ObjectCollection;
	import com.flyee.service.pool.RelationMV;
	import com.flyee.utils.Constants;
	import com.flyee.utils.CoodinateUtil;
	import com.flyee.utils.Obsevable;
	import com.flyee.utils.Obsever;
	import com.flyee.viewer.MonsterViewer;
	
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	
	public class MonsterController extends Obsevable implements Obsever{
		
		[Inject]
		public var characterService : CharactorService;
		[Inject]
		public var cartoonController : CharacterCartoonController;
		[Inject]
		public var moveController : CharactorMoveController;
		
		private var checkTimer : Timer;
		
		public function MonsterController(){

		}
		
		public function execute(o : Obsevable, command : String=null, args : * = null) : void {
			switch(command){
				case "joinFight" : 
					joinFight(args[0], args[1]);
					break ;
				default :
					break;
			}
		}
		
		public function init() : void {
			checkTimer = new Timer(20000);
			checkTimer.addEventListener(TimerEvent.TIMER, checkMonstersHandler);
			//初试化怪物
			for(var i : int = 0 ; i < 10 ; i++){
				addMonster("m" + checkTimer.currentCount + i, Constants.MONSTER_BORN_POINT[i][0], Constants.MONSTER_BORN_POINT[i][1]);
			}
			checkTimer.start();
		}
		
		public function checkMonstersHandler(event : TimerEvent) : void {
			//j检查怪物死亡情况
			for each(var monster : Monster in ObjectCollection.monsterMap.values()){				
				if(monster.hp==0){
					if(monster == ObjectCollection.master.target)
						ObjectCollection.targetWindowViewer.visible = false;
//					ObjectCollection.monsterMap.remove(monster.mid);
//					ObjectCollection.charactorMap.remove(monster.mid);
					disposeDead(monster);	
//					addMonster(monster.gcode, monster.bornX, monster.bornY+22);
				}			
			}
		}
		
		public function addMonster(gcode : String, x : Number, y : Number) : void {
			var monster : Monster = new Monster();
			monster.gcode = gcode;
			monster.name = "小怪兽";
			monster.hp = 500;
			monster.mp = 0;
			monster.mySex = 0;
			monster.sceneId = ObjectCollection.currentScene.id;
			monster.sceneInstanceId = ObjectCollection.currentScene.id;
			monster.x = x;
			monster.y = y-22;
			monster.bornX = x;
			monster.bornY = y-22;
			var monsterViewer : MonsterViewer = new MonsterViewer();
			monsterViewer.setNameText(monster.name);
			var bmp : Bitmap = new Bitmap(EffectDepot.loadingPlayer.bitmapData);			
			monsterViewer.charactorContainer.addChild(bmp);
//			bmp.x = -110;
//			bmp.y = 60;
			moveController.addCharactor(monster, monsterViewer, ObjectCollection.currentSceneViewer.componentLayer);
//			monsterViewer.charactorContainer.x = 10;
//			monsterViewer.charactorContainer.y = -40;
			loadMonsterPlate(monster, monsterViewer);	
			monsterViewer.addEventListener(MouseEvent.CLICK, cartoonController.chooseTarget);
			
			ObjectCollection.monsterMap.put(monster.mid, monster);
			ObjectCollection.charactorMap.put(monster.mid, monster);
			ObjectCollection.monsterViewerMap.put(monsterViewer.vid, monsterViewer);
			ObjectCollection.charactorViewerMap.put(monsterViewer.vid, monsterViewer);
			ObjectCollection.relationArray.push(new RelationMV(monster.mid, monsterViewer.vid));
		}
		
		private function loadMonsterPlate(monster : Monster, monsterViewer : MonsterViewer) : void {			
			
		 	var attackSkin:BitmapData = MonsterDepot.getBeeAttack();
			var moveSkin:BitmapData = MonsterDepot.getBeeMove();
			var stopSkin:BitmapData = MonsterDepot.getBeeStop();
			//裁剪战斗动作 
			var tempArray:Array = CutMananger.cutBitmapData(attackSkin);
			monsterViewer.plateArray[AppearanceConfig.MOVE_BY_FIGHT] = tempArray;
			//裁剪移动动作
			var tempArray2:Array = CutMananger.cutBitmapData(moveSkin);
			monsterViewer.plateArray[AppearanceConfig.MOVE_BY_WALK] = tempArray2;
			//裁剪站立动作
			var tempArray3:Array = CutMananger.cutBitmapData(stopSkin);
			monsterViewer.plateArray[AppearanceConfig.MOVE_BY_STAND] = tempArray3;
			
			var cartoonTimer : CartoonTimer = null;
			if(!ObjectCollection.cartoonTimerMap.containsKey(monsterViewer.vid)){

				cartoonTimer = new CartoonTimer(monster, monsterViewer, Constants.FRAME_ACTION_TIME);
				ObjectCollection.cartoonTimerMap.put(monsterViewer.vid, cartoonTimer);
			}else{
				cartoonTimer = ObjectCollection.cartoonTimerMap.get(monsterViewer.vid);
			}
			cartoonTimer.autoPlay = true;
			cartoonTimer.setCurrentStatus(1,0,0);
			cartoonTimer.timerHandle();	
			monsterViewer.nameText.x = monsterViewer.width / 2 - monsterViewer.nameText.width / 2;	
		}
		
		public function joinFight(player : Charactor, monster : Monster) : void {
			var monTimer : MonsterTimer = ObjectCollection.monsterTimerMap.get(monster.mid);
			if(monTimer == null) {
				monTimer = new MonsterTimer(1000);
				monTimer.attackFn = attack;
				monTimer.checkFn = check;
				monTimer.trackFn = track;
				monTimer.monster = monster;
				ObjectCollection.monsterTimerMap.put(monster.mid, monTimer);
			}
			
			monster.target = player;			
			monTimer.start();
		}
		
		public function check(mon : Monster) : int {
			if(mon == null || mon.target == null) return -1;
			if(mon.hp == 0 || mon.target.hp == 0) return -1;
			var dis : Number = CoodinateUtil.checkDistance(mon.x, mon.y, mon.target.x, mon.target.y);
			if(dis<=1) return 0;
			if(dis>1&&dis<12) return 1;
			if(dis>=12) return 2;
			return -1;
		}
		
		public function attack(mon : Monster) : void {
			characterService.executeSkill(mon.gcode, mon.target.gcode, 1, 10);
		}
		
		public function track(mon : Monster) : void {
			var pathArr : Array = AStar.findPath(mon.y+22, mon.x, mon.target.y+22, mon.target.x);
			if(pathArr==null) return ;
			pathArr.splice(1, pathArr.length - 1);
//			trace("monster path length : " + pathArr.length);
			characterService.move(mon.gcode, mon.x, mon.y+22, pathArr);
		}
			
		public function disposeDead(monster : Monster) : void {
			ObjectCollection.monsterMap.remove(monster.mid);
			ObjectCollection.charactorMap.remove(monster.mid);
			var monV : MonsterViewer = ObjectCollection.getCharacterViewerByMid(monster.mid) as MonsterViewer;
			monster.target = null;
			if(monV == null) return;
			ObjectCollection.charactorViewerMap.remove(monV.vid);
			ObjectCollection.monsterViewerMap.remove(monV.vid);
			ObjectCollection.removeRelationByMid(monster.mid);
						
			if(ObjectCollection.moveTimerMap.containsKey(monV.vid)){
				var moveTimer : WalkTimer = ObjectCollection.moveTimerMap.remove(monV.vid) as WalkTimer;
				moveTimer.dispose();
			}
			if(ObjectCollection.cartoonTimerMap.containsKey(monV.vid)){
				var cartoonTimer : CartoonTimer = ObjectCollection.cartoonTimerMap.remove(monV.vid) as CartoonTimer;
				cartoonTimer.dispose();
			}
			if(ObjectCollection.currentSceneViewer.componentLayer.contains(monV))
				ObjectCollection.currentSceneViewer.componentLayer.removeChild(monV);
			if(ObjectCollection.monsterTimerMap.containsKey(monV.vid)){
				var monTimer : MonsterTimer = ObjectCollection.monsterTimerMap.remove(monV.vid) as MonsterTimer;
				monTimer.dispose();				
			}
			monV.dispose();
			addMonster(monster.gcode, monster.bornX, monster.bornY+22);
		}

	}
}